package com.haihui.platform.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.haihui.platform.entity.DatatablesViewPage;
import com.haihui.platform.entity.PagingBean;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.haihui.platform.entity.CommResultMsg;
import com.haihui.platform.utils.JsonUtils;

/**
 * Created by HaiHui on 2017/9/12.
 */
public abstract class BaseController {
    @SuppressWarnings("unused")
	private static final String FILTERED_REQUEST = "@@session_context_filtered_request";
    protected Logger logger = Logger.getLogger(BaseController.class);
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected static String GRID_TYPE_DATATABLES = "datatables";
    protected static String GRID_TYPE_JQGRID = "jqGrid";

    public BaseController() {
    }

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }


    public void responseWriter(String jsonString) {
        this.writeData(jsonString, "application/json");
    }

    public void writeTextJson(String jsonString) {
        this.writeData(jsonString, "text/json");
    }

    public void writeData(String jsonString, String contentType) {
        this.response.setContentType(contentType);
        PrintWriter out = null;

        try {
            this.response.setCharacterEncoding("utf-8");
            out = this.response.getWriter();
            out.write(jsonString);
        } catch (Exception var8) {
            this.logger.error(var8.getMessage(), var8);
        } finally {
            out.flush();
            out.close();
        }

    }
    public<T> String getCommonJsonPage(int count,T t){
        DatatablesViewPage pageCommon =  new DatatablesViewPage();
        pageCommon.setiTotalDisplayRecords(count);
        pageCommon.setiTotalRecords(count);
        pageCommon.setAaData(t);
        return JsonUtils.getJAVABeanJSON(pageCommon);
    }

    @SuppressWarnings("rawtypes")
	public String getCommonJsonString(Boolean result, String msg) {
        CommResultMsg comm = new CommResultMsg();
        comm.setResult(result);
        comm.setDescription(msg);
        return JsonUtils.getJAVABeanJSON(comm);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public <T> String getCommonJsonString(Boolean result, String msg, T t) {
        CommResultMsg comm = new CommResultMsg();
        comm.setResult(result);
        comm.setDescription(msg);
        comm.setData(t);
        return JsonUtils.getJAVABeanJSON(comm);
    }
    public PagingBean getPagingBean() {
        PagingBean pagingBean = null;
        String startString = this.request.getParameter("start");
        String lenString = this.request.getParameter("length");
        Integer start = Integer.valueOf(0);
        Integer limit = Integer.valueOf(10);
        if(startString != null) {
            start = Integer.valueOf(startString);
        }

        if(lenString != null) {
            limit = Integer.valueOf(lenString);
        }

        pagingBean = new PagingBean(start.intValue() / limit.intValue() + 1, limit.intValue());


        return pagingBean;
    }

}
